local fuju = fk.CreateSkill{
  name = "rfenghou_3d9j__fujudamage",
}

local DIY = require "packages.diy_utility.diy_utility"

Fk:addQmlMark{
  name = "rfenghou_3d9j__fujudamage",
  how_to_show = function(name, value, player)
    if type(value) ~= "table" then return " " end
    for _, kv in ipairs(value) do
      if kv[1] == Self then
        return Fk:translate(kv[2])
      end
    end
    return " "
  end
}

fuju:addEffect(fk.Damaged, {
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local num = math.ceil(DIY.getSwitchState(player, "rfenghou_3d9j__fujuswitch") / 2)
    local _, dat = room:askToUseActiveSkill(player, {
      skill_name = "#rfenghou_3d9j__fujuactive",
      prompt = "#rfenghou_3d9j__fujudamage-invoke",
      extra_data = {
        skillName = fuju.name,
        num = num
      }
    })
    if dat then
      event:setCostData(self, { tos = dat.targets, choice = dat.interaction, num = num })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    local to, choice, num = dat.tos[1], dat.choice, dat.num
    -- 重量级之技能二合一
    if choice:startsWith(Fk:translate("drawX"):sub(1, 1)) then
      local before = to:getHandcardNum()
      to:drawCards(num, fuju.name)
      if math.abs(to:getHandcardNum() - before) > before then
        room:handleAddLoseSkills(player, "-" .. fuju.name)
        room:addTableMark(to, "@[rfenghou_3d9j__fujudamage]rfenghou_3d9j__fujudamage", {player, "#rfenghou_3d9j__fujudamage_tokill"})
      end
    else
      local before = to:getHandcardNum()
      room:askToDiscard(to, {
        min_num = num,
        max_num = num,
        include_equip = true,
        skill_name = fuju.name,
        cancelable = false,
      })
      if math.abs(to:getHandcardNum() - before) > to:getHandcardNum() then
        room:handleAddLoseSkills(player, "-" .. fuju.name)
        room:addTableMark(to, "@[rfenghou_3d9j__fujudamage]rfenghou_3d9j__fujudamage", {player, "#rfenghou_3d9j__fujudamage_bekill"})
      end
    end
  end
})
fuju:addEffect(fk.Death, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:getMark("@[rfenghou_3d9j__fujudamage]rfenghou_3d9j__fujudamage") ~= 0 and (player == target or player == data.killer)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to_restore = {}
    for _, kv in ipairs(target:getTableMark("@[rfenghou_3d9j__fujudamage]rfenghou_3d9j__fujudamage")) do
      if kv[1] and not kv[1].dead and kv[2] == "#rfenghou_3d9j__fujudamage_bekill" then
        table.insert(to_restore, {kv, target})
      end
    end
    if data.killer then
      for _, kv in ipairs(data.killer:getTableMark("@[rfenghou_3d9j__fujudamage]rfenghou_3d9j__fujudamage")) do
        if kv[1] and not kv[1].dead and kv[2] == "#rfenghou_3d9j__fujudamage_tokill" then
          table.insert(to_restore, {kv, data.killer})
        end
      end
    end
    for _, kv2 in ipairs(to_restore) do
      room:removeTableMark(kv2[2], "@[rfenghou_3d9j__fujudamage]rfenghou_3d9j__fujudamage", kv2[1])
      room:handleAddLoseSkills(kv2[1][1], fuju.name)
    end
  end,
})

Fk:loadTranslationTable{
  ["rfenghou_3d9j__fujudamage"] = "复炬",
  [":rfenghou_3d9j__fujudamage"] = "当你受到伤害时，你可以令一名角色摸/弃置X张牌（X为“拂局”状态编号的一半，向上取整），若其此次以此法操作的牌数大于其之前/当前的牌数，你失去“复炬”直到其杀死角色/死亡。",

  ["@[rfenghou_3d9j__fujudamage]rfenghou_3d9j__fujudamage"] = "复炬",
  ["#rfenghou_3d9j__fujudamage_tokill"] = "杀人",
  ["#rfenghou_3d9j__fujudamage_bekill"] = "死亡",
}

return fuju
